//
//  M2HelpView.m
//  M2Common
//
//  Created by Chen Meisong on 13-7-17.
//  Copyright (c) 2013年 Chen Meisong. All rights reserved.
//

#define M2HelpView_UserDefaultsKey_CFBundleVersionVersion @"M2HelpView_UserDefaultsKey_CFBundleVersionVersion"

#import "M2HelpView.h"

@implementation M2HelpView

+ (BOOL)hasShowHelp{
    NSString *version = [[NSUserDefaults standardUserDefaults] objectForKey:M2HelpView_UserDefaultsKey_CFBundleVersionVersion];
    if (!version) {
        return NO;
    }
    
    NSString *nowVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
    if (![version isEqualToString:nowVersion]) {
        return NO;
    }
    
    return YES;
}
- (id)initWithImageNames:(NSArray*)imageNames{
    CGRect frame = [UIScreen mainScreen].bounds;
    frame = CGRectMake(0.0, 0.0, frame.size.height, frame.size.width);
    self = [super initWithFrame:frame];
    if (self) {
        if (!imageNames || imageNames.count <= 0) {
            return self;
        }
        
        float width = self.bounds.size.width;
        float height = self.bounds.size.height;
        
        UIScrollView *container = [[UIScrollView alloc] initWithFrame:self.bounds];
        container.contentSize = CGSizeMake(width * imageNames.count, height);
        container.showsHorizontalScrollIndicator = NO;
        container.showsVerticalScrollIndicator = NO;
        container.pagingEnabled = YES;
       
        UIImageView *imgV = nil;
        for (int i = 0; i < imageNames.count; i++) {
            imgV = [[UIImageView alloc] initWithFrame:CGRectMake(width * i, 0.0, width, height)];
            imgV.image = [UIImage imageNamed:imageNames[i]];
            imgV.userInteractionEnabled = YES;
            
            if (i == imageNames.count - 1) {
                UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
                closeButton.frame = CGRectMake((imgV.bounds.size.width - M2HelpView_CloseButton_Width) / 2.0, imgV.bounds.size.height - M2HelpView_CloseButton_BottomMargin - M2HelpView_CloseButton_Height, M2HelpView_CloseButton_Width, M2HelpView_CloseButton_Height);
                closeButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
                [closeButton addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
                [imgV addSubview:closeButton];
                
                // 校准关闭按钮frame时使用
//                closeButton.backgroundColor = [UIColor blueColor];
//                closeButton.alpha = 0.5;
            }
            
            [container addSubview:imgV];
            [imgV release];
        }
        
        [self addSubview:container];
        [container release];
    }
    
    return self;
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
}

- (void)onClick{
    
    //NSUserDefauts 用于保存应用程序设置和属性以及用户数据
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *nowVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
    [userDefaults setObject:nowVersion forKey:M2HelpView_UserDefaultsKey_CFBundleVersionVersion];
    [userDefaults synchronize];
    
    [self removeFromSuperview];
}

@end
